﻿using System;
using System.Collections.Generic;
using System.Text;
using XmlLinq;
using System.Xml;

namespace SerialPorts
{
    public class SerialRequest
    {
        public string sendStr;
        public string TypeRequest;
        public string ID;
        public XElement packgaeNode;
        public XmlNode rootNode;
        public string Error;

        public SerialRequest()
        {
             
        }
        public virtual bool CreatPackage()
        {
            packgaeNode = new XElement("package",new XAttribute("version","1.0"));
            XElement taskNode = new XElement("task", new XAttribute("id", ID),
                                                     new XAttribute("type", TypeRequest));
            packgaeNode.Add(taskNode);
            SendStr = packgaeNode.ToString();
            return true;
        }
        public bool ParseReplyPackage(string replayDate)
        {
            if (string.IsNullOrEmpty(replayDate))
                return false;
            try
            {
                XmlDocument redoc = new XmlDocument();
                redoc.LoadXml(replayDate);

                rootNode = redoc.SelectSingleNode("/package/task");
                if (rootNode == null)
                    return false;
                string id = rootNode.Attributes["id"].Value;
                string type = rootNode.Attributes["type"].Value;
                string result = rootNode.Attributes["status"].Value;
                Error = rootNode.Attributes["error"].Value;
                if (id != ID||type != TypeRequest || result != "SUCCESS")
                    return false;


            }
            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
        public string SendStr
        {
            get { return sendStr; }
            set { sendStr = value; }

        }
    }
    public class SerialSwitchRequest : SerialRequest
    {
        public string Swtch;
        public SerialSwitchRequest(string swtch)
        {
            TypeRequest = "SWITCH";
            Swtch = swtch;
            ID = "0";                                                      
        }
        public override bool CreatPackage()
        {
            if (!base.CreatPackage())
                return false;
            XElement taskNode = packgaeNode.Element("task");
            taskNode.Remove();
            XAttribute swAttribute = new XAttribute("switch", Swtch);
            taskNode.Add(swAttribute);
            packgaeNode.Add(taskNode);
            SendStr = packgaeNode.ToString();
            return true;
        }
    }
    public class SerialBrightRequest : SerialRequest
    {
        public string Bright;
        public SerialBrightRequest(string bright)
        {
            TypeRequest = "BRIGHTNESS";
            Bright = bright;
            ID = "1"; 
        }
        public override bool CreatPackage()
        {
            if (!base.CreatPackage())
                return false;
            XElement taskNode = packgaeNode.Element("task");
            taskNode.Remove();
            XAttribute swAttribute = new XAttribute("brightness", Bright);
            taskNode.Add(swAttribute);
            packgaeNode.Add(taskNode);
            SendStr = packgaeNode.ToString();
            return true;
        }
    }
    public class SerialRebootRequest : SerialRequest
    {
        public SerialRebootRequest()
        {
            TypeRequest = "REBOOT";
            ID = "2"; 
        }
    }
}
/*
<package version="1.0">
		<task id="0" type="SWITCH" switch="1"></task>
</package>
 * 
 <package version=”1.0”>
		<task id=”0” type=” SWITCH” status=”SUCCESS” error=”SUCCESS”></task>
</package>

 * 
 1)	开关屏
		<task id=”...” type=”SWITCH” switch=”0”></task>

2)	亮度设置
	<task id=”...” type=”BRIGHTNESS” brightness=”0”></task>

3)	系统重启
	<task id=”...” type=”REBOOT”></task>

 */
